home *** CD-ROM | disk | FTP | other *** search
Text File | 1990-05-04 | 3.0 KB | 75 lines | [TEXT/GEOL] |
- Item 1993529 29-April-90 16:52PDT
-
- From: D3632 Cadence Design, Ken Friedenbach,PRT
-
- To: DEREK White, Derek
- MIKE.VILOT ObjectWare, Michael Vilot,PRT
- D0532 Aidea Systems, Don Park,PRT
-
- cc: CPLUS.APPLE$ C++ Interest List--Apple Employees
- CPLUS.DEV$ C++ Interest List--Developers
-
- Sub: RE 2-Value Parameters
-
- Dear Derek, Mike, Don,
-
- The problem that Derek points out is an optimization problem, but not a
- correctness problem. The call is being made as a virtual function call, even
- though it could be staticly bound at compile time. But with either calling
- mechanism, the semantics will be the same: TShape::Draw() will be called. So
- CFront is not the world's greatest optimizing compiler! What else is new? We
- should be thankful that is (close?) to being correct!
-
- The actual parameter value is used to initialize the formal parameter copy.
- The compiler will use the construtor:
- TShape::TShape(const TShape&)
- to do the initialization. If the user does not define this constructor, the
- compiler will construct one. (Why? Because C supported initialization and
- assignment as built-ins.)
-
-
- Don's comments addressed the way in which the pointer to the vtable for
- TShape:: is used to initialize the copy. It is correct, and important, to use
- the vtable for TShape::, not the vtable for the actual parameter. If the
- vtable of the actual parameter type was used (say TRoundRect) then code that is
- called would end up addressing data that is not part of TShape (say the
- ovalWidth and ovalHeight fields, which control the "rounding" of the corners).
- Multiple inheritance would present a similiar class of problems.
-
- So passing by value (which is part of C) is mutually exclusive with
- polymorphism. The "types" still match, but the value is being "narrowed" back
- to a true TShape (just as an int will be narrowed to a short or char). This
- "narrowing" must include going back to the virtual functions for the base
- class, since it is a base class (sized) object which is being passed.
-
-
- Mike's comments about using references are correct. Polymorphism in C++ is
- limited to pointers and references. (That is why "this" is of type X* rather
- than X.) The choice between pointer and reference is a tough one. References
- are clearly necessary for operators such as =, and += to be defined for user
- defined types, since we want to write:
- x = y = z;
- not
- &x = &y = z;
- which we would have to do for global operators which take pointers.
-
-
- Note: There are some disadvantages in associating polymorphism with pointers.
- In C, pointer arithmetic is defined:
- TAType * P = AArray[0];
- ...
- P++; // next element
- P += 10; // move 10 elements in array
- However, what does this mean:
- TShape * S;
- // S is used as a "polymorphic" pointer
- S++; // probably a bug!
- S+=3; // ditto
-
-
- Virtually yours,
-
- Ken
-
-
-